package com.example.hutooldemo;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @author by linenxi
 * @Classname ExcelUtil
 * @Description
 * @Date 2019/5/8
 */
@Component
public class ExcelUtil {

    @Autowired
    private OssUtil ossUtil;

    /**
     * 将传过来的对象进行excel导出,并放入oss中返回链接
     *
     * @return
     */
    public String getExcelOssUrl(List javaExport, List<String> heads, String prefixName, String fileName) {
        // 通过工具类创建writer
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter();
        fillHeaderAliasFileName(writer, TestBean.class, heads, fileName);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(javaExport, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        writer.flush(byteArrayOutputStream);
        byte[] bytes = byteArrayOutputStream.toByteArray();
        // 关闭writer，释放内存
        writer.close();
        IoUtil.close(byteArrayOutputStream);
        return ossUtil.uploadFile(prefixName, ".xls", bytes);
    }

    /**
     * 将传过来的对象进行客户端导出
     *
     * @return
     */
    public void getExcelClient(List javaExport, List<String> heads, String fileName) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        // 通过工具类创建writer
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter();
        fillHeaderAliasFileName(writer, TestBean.class, heads, fileName);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(javaExport, true);
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.flush(outputStream);
            // 关闭writer，释放内存
            writer.close();
            IoUtil.close(outputStream);
        }
    }

    private void fillHeaderAliasFileName(ExcelWriter writer, Class clazz, List<String> heads, String fileName) {
        Field[] declaredFields = clazz.getDeclaredFields();
        Assert.state(declaredFields.length == heads.size(), "字段错误");
        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(heads.size()-1, fileName);
        for (int i = 0; i < declaredFields.length; i++) {
            writer.addHeaderAlias(declaredFields[i].getName(), heads.get(i));
            Console.log("name:[ {} ], head:[ {} ]", declaredFields[i].getName(), heads.get(i));
        }
    }
}
